﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using cyq_dnetmvc.Models;

namespace cyq_dnetmvc.Controllers
{
    public class DemoController : Controller
    {
        private cyq_dbEntities db = new cyq_dbEntities();

        //
        // GET: /Demo/

        public ActionResult Index()
        {
            return View(db.topictable.ToList());
        }

        //
        // GET: /Demo/Details/5

        public ActionResult Details(int id = 0)
        {
            topictable topictable = db.topictable.Single(t => t.topic_id == id);
            if (topictable == null)
            {
                return HttpNotFound();
            }
            return View(topictable);
        }

        //
        // GET: /Demo/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Demo/Create

        [HttpPost]
        public ActionResult Create(topictable topictable)
        {
            if (ModelState.IsValid)
            {
                db.topictable.AddObject(topictable);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(topictable);
        }

        //
        // GET: /Demo/Edit/5

        public ActionResult Edit(int id = 0)
        {
            topictable topictable = db.topictable.Single(t => t.topic_id == id);
            if (topictable == null)
            {
                return HttpNotFound();
            }
            return View(topictable);
        }

        //
        // POST: /Demo/Edit/5

        [HttpPost]
        public ActionResult Edit(topictable topictable)
        {
            if (ModelState.IsValid)
            {
                db.topictable.Attach(topictable);
                db.ObjectStateManager.ChangeObjectState(topictable, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(topictable);
        }

        //
        // GET: /Demo/Delete/5

        public ActionResult Delete(int id = 0)
        {
            topictable topictable = db.topictable.Single(t => t.topic_id == id);
            if (topictable == null)
            {
                return HttpNotFound();
            }
            return View(topictable);
        }

        //
        // POST: /Demo/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            topictable topictable = db.topictable.Single(t => t.topic_id == id);
            db.topictable.DeleteObject(topictable);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}